<template>
  <div ref="indexLine" class="echats"></div>
</template>

<script>
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    indexData: Array,
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    indexData: {
      handler() {
        this.initChart();
      },
      deep: true,
    },
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      let echarts = require("echarts");
      // 基于准备好的dom，初始化echarts实例
      let mychart = this.$refs.indexLine;
      this.chart = echarts.init(mychart);
      let option = {
        color: ["#3aa1ff"],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        grid: {
          top: 10,
          left: "2%",
          right: "2%",
          bottom: 40,
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: ["12月17日", "12月18日", "12月19日", "12月20日", "12月21日", "12月22日", "12月23日"],
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
              textStyle: {
                color: "#666666",
              }
            },
            axisLine: {
              lineStyle: {
                color: "#d9d9d9"
              }
            }
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLabel: {
              textStyle: {
                color: "#666666",
                margin: 2
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              lineStyle: {
                color: "#f1f1f1",
                type: "dashed"
              }
            }
          },
        ],
        series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line'
        }]
      };
      // 绘制图表
      this.chart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
.echats {
  width: 100%;
  height: 100%;
}
</style>
